// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-25 19:01
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain.preFixTrain;

import org.junit.jupiter.api.Test;

public class MaxAbsoluteSum {
    /**
     * 最大子数组绝对值之和 -> 双指针 + 前缀和
     *
     * @param nums
     * @return
     */
    public int maxAbsoluteSum(int[] nums) {
        int n = nums.length, ans = 0;
        if (n == 1) {
            return Math.abs(nums[0]);
        }
        int[] sum = new int[n + 1];
        int min = 0, max = 0;
        for (int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + nums[i - 1];
        }
        for (int i = 0; i < n + 1; i++) {
            ans = Math.max(ans, Math.abs(sum[i] - min));
            ans = Math.max(ans, Math.abs(max - sum[i]));
            min = Math.min(min, sum[i]);
            max = Math.max(max, sum[i]);
        }

        return ans;
    }


    @Test
    public void shout() {
        int[] t = {2, -1};
        System.out.println(maxAbsoluteSum(t));
    }
}
